/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.optimus.db.dao.impl;

import com.optimus.common.util.ApplicationException;
import com.optimus.common.vo.BaseVO;
import com.optimus.common.vo.DistanciaVO;
import com.optimus.db.QueryFactory;
import com.optimus.db.dao.DistanciaDAO;
import java.math.BigDecimal;
import java.math.RoundingMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author vperez
 */
public class DistanciaDAOImpl extends BaseDAO implements DistanciaDAO {

    private static  Logger logger = LoggerFactory.getLogger(DistanciaDAOImpl.class);
    
    public BaseVO insertar(DistanciaVO distanciaVO) throws ApplicationException {
        Object[] logParams = new Object[2];
        BaseVO result = new BaseVO();
        Long tk = distanciaVO.getTicket();
        result.setTicket(tk);
        try {

            String queryStr=QueryFactory.instance().query("distancia_insertar", tk);
            logParams[0] = queryStr;
            Session session_db= session();
            session_db.beginTransaction();
            SQLQuery query = session_db.createSQLQuery(queryStr);
            query.setInteger("inicio", distanciaVO.getInicio());
            query.setInteger("termino", distanciaVO.getTermino());
            BigDecimal  dist=distanciaVO.getDistancia();
            dist=dist.setScale(10,RoundingMode.HALF_EVEN);
            query.setBigDecimal("distancia", dist);
            query.setString("escala", distanciaVO.getScala());
            //Ejecutar
            Integer objList = query.executeUpdate();
            session_db.getTransaction().commit();
        } catch (Exception e) {
            throw new ApplicationException("MDB0000", e);
        } finally {
            logger.info("sql={} ", logParams);
        }
        return result;
    }

    public BaseVO borrarDistancias(BaseVO baseVO) throws ApplicationException {
        Object[] logParams = new Object[2];
        BaseVO result = new BaseVO();
        Long tk = baseVO.getTicket();
        result.setTicket(tk);
        try {

            String queryStr=QueryFactory.instance().query("distancia_eliminar", tk);
            logParams[0] = queryStr;
            Session session_db= session();
            session_db.beginTransaction();
            SQLQuery query = session_db.createSQLQuery(queryStr);
            Integer objList = query.executeUpdate();
            session_db.getTransaction().commit();
        } catch (Exception e) {
            throw new ApplicationException("MDB0000", e);
        } finally {
            logger.info("sql={} ", logParams);
        }
        return result;
    }

}
